package com.example.orderflow.controller.flow;

import com.example.orderflow.controller.base.ABaseController;
import com.example.orderflow.entity.constants.ApproveHistoryConstants;
import com.example.orderflow.entity.vo.ResponseVO;
import com.example.orderflow.service.ApprovalStageService;
import com.example.orderflow.service.ApproveHistoryService;
import com.example.orderflow.service.OrderInfoService;
import com.example.orderflow.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static com.example.orderflow.entity.constants.OrderInfoConstants.APPROVE_STATUS_RETURN;
import static com.example.orderflow.entity.constants.OrderInfoConstants.ORDER_STATU_RETURN;

/**
 * 审核阶段：订单审核1
 * 审核等级：3
 */

@RestController
public class OrderApprovalSection1Controller extends ABaseController {

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    ApprovalStageService approvalStageService;

    @Autowired
    ApproveHistoryService approveHistoryService;

    @Autowired
    OrderInfoService orderInfoService;

    /**
     * 订单审核1 通过 3
     * @param map
     * orderId 订单编号
     * approveBy 审批人
     * @return
     */
    @PostMapping("/orderApprovalSection1Pass")
    protected <T> ResponseVO orderApprovalSection1Pass(@RequestBody Map<String,String> map) {

        String orderId = map.get("orderId"); //订单号
        String userName = map.get("userName"); //审批人
        String comment = map.get("comment"); //意见
        int levelId = 3;
        String levelIdS = "3";

        //核实订单号和审批人

        //找到下一个等级的名称
        String NextLevelname = approvalStageService.getApprovalStageByApprovalId(levelId+1).getLevelName();

        //更改单人审核状态
        approveHistoryService.updateCommentApproveHistory(orderId,levelIdS,userName,ApproveHistoryConstants.APPROVE_STATUS_PASS,comment);

        //查询这一步骤全部状态
        if (approveHistoryService.isAllApproveStatus(orderId,levelIdS)){

            orderInfoService.updateOrderInfoLevelName(orderId,NextLevelname);

            approveHistoryService.createDefaultApproveHistory(orderId,String.valueOf(levelId+1),"李春联");

            return getSuccessResponseVO("该订单订单审批1阶段已完成");
        }

        return getSuccessResponseVO("审核成功");
    }

    /**
     * 订单审核1 退回 3
     * @param map
     * orderId 订单编号
     * approveBy 审批人
     * @return
     */
    @PostMapping("/orderApprovalSection1Return")
    protected <T> ResponseVO orderApprovalSection1Return(@RequestBody Map<String,String> map) {

        String orderId = map.get("orderId"); //订单号
        String approveBy = map.get("userName"); //审批人
        int levelId = 3;
        String levelIdS = "3";

        //核实订单号和审批人

        //获取
        String Levelname = approvalStageService.getApprovalStageByApprovalId(levelId).getLevelName();

        //更新审核记录表该审核人审核状态为退回
        approveHistoryService.updateDefaultApproveHistory(orderId,levelIdS,approveBy,"退回");

        //更新全部审核记录，记录状态为0
        approveHistoryService.updateObjectStateAndApproveTimeByOrderId(orderId);

        //审核状态改为已退回
        orderInfoService.updateApproveStatusByOrderIdAndApproveStatus(orderId,APPROVE_STATUS_RETURN);

        //订单状态改为废弃
        orderInfoService.updateOrderStatuAndVoidedByByOrderIdAndOrderStatuAndVoidedBy(orderId,ORDER_STATU_RETURN,approveBy);

        return getSuccessResponseVO("退回订单"+orderId+"成功,订单已废弃");
    }


}
